//
//  CSTextField.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/10/31.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSTextField.h"

@interface CSTextField () <UITextFieldDelegate>

@property (nonatomic, strong) UIColor * originLineColor;
@property(nonatomic, assign) float originLineWidth;

@end

@implementation CSTextField


+ (instancetype)gx_textFieldWithLineColor:(UIColor *)originLineColor lineWidth:(CGFloat)lineWidth {
    CSTextField * txt = [[self alloc] init];
    originLineColor = !originLineColor ? [UIColor grayColor] : originLineColor;
    txt.lineColor = txt.originLineColor = originLineColor;
    txt.lineWidth = txt.originLineWidth = lineWidth;
    return txt;
}

- (void)setupUI {
    self.borderStyle = UITextBorderStyleNone;
    self.font = [UIFont systemFontOfSize:13];
    
    UIView * vPlace = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2, 1)];
    self.leftView = vPlace;
    self.leftViewMode = UITextFieldViewModeAlways;
    
    self.delegate = self;
}

- (void)textFieldDidBeginEditing:(CSBaseTextField *)textField {
    self.lineColor = (self.selectedLineColor) ? self.selectedLineColor : [UIColor colorWithRed:60/255.0 green:191/255.0 blue:243/255.0 alpha:1];
    self.lineWidth = 3;
}

- (void)textFieldDidEndEditing:(CSBaseTextField *)textField {
    self.lineColor = self.originLineColor;
    self.lineWidth = self.originLineWidth;
}

- (void)drawRect:(CGRect)rect {
    
    self.lineColor = !self.lineColor ? [UIColor blackColor] : _lineColor;
    self.lineWidth = (self.lineWidth == 0) ? 2 : self.lineWidth;
    
    UIBezierPath * path = [UIBezierPath bezierPath];
    path.lineWidth = self.lineWidth;
    
    CGPoint pointBegin = CGPointMake(0, self.bounds.size.height * 2/3);
    CGPoint pointEnd = CGPointMake(self.bounds.size.width, self.bounds.size.height * 2/3);
    
    [path moveToPoint:pointBegin];
    [path addLineToPoint:CGPointMake(0, self.bounds.size.height * (1 - 1.f/6))];
    [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height * (1 - 1.f/6))];
    [path addLineToPoint:pointEnd];
    
    [self.lineColor set];
    
    [path stroke];
}

#pragma mark - setter & getter
- (void)setLineColor:(UIColor *)lineColor {
    _lineColor = lineColor;
    
    [self setNeedsDisplay];
}

- (void)setLineWidth:(float)lineWidth {
    _lineWidth = lineWidth;
    [self setNeedsDisplay];
}

@end
